<template>
	<view class="addInspectTabsContent">
		<view class="upper">
			<commonNavBar title="结果记录表">
				<template v-slot:right>
					<image src="/static/img/save.png" @tap="saveImage" style="width: 45rpx;height: 45rpx;"></image>
				</template>
			</commonNavBar>
			<scroll-view id="content" :scroll-y="true" :style="{ height: contentHeight + 'px' }">
				<view id="scrrent-page" class="list">
					<view class="title-view"> <text class="title-text">食品经营者日常监督检查结果记录表</text> </view>
					<view class="title-view" :style="{ 'text-align': 'right', 'padding-right': '5px' }"> <text
							class="title-text">编号：{{ checkData.resultCode }}</text> </view>
					<table class="content-table" celspacing="0">
						<tr>
							<td class="content-td" width="90rpx">被检查单位名称</td>
							<td class="content-td" width="100rpx">{{ checkData.COMPANY_NAME }}</td>
							<td class="content-td" width="90rpx">经营地址</td>
							<td class="content-td" width="100rpx">{{ checkData.ADDRESS }}</td>
						</tr>
						<tr>
							<td class="content-td" width="90rpx">联系人</td>
							<td class="content-td" width="100rpx">{{ checkData.CONTACT }}</td>
							<td class="content-td" width="90rpx">联系方式</td>
							<td class="content-td" width="100rpx">{{ checkData.CONTACT_PHONE }}</td>
						</tr>
						<tr>
							<td class="content-td" width="90rpx">业态</td>
							<td class="content-td" colspan="3">{{ checkData.BUSINESS_TYPE }}</td>
						</tr>
						<tr>
							<td class="content-td" width="90rpx">许可证（备案）编号或社会信用代码</td>
							<td class="content-td" colspan="3">{{ checkData.LICENSE }}</td>
						</tr>
						<tr>
							<td class="content-td" colspan="4">
								<view>检查内容：</view>
								<text class="check-content">　
									　{{ checkData.DEPT_NAME }}　　　　　　　　　
									根据《中华人民共和国食品安全法》及其实施条例、《食品生产经营者日常监督检查管理办法》、《食用农产品市场销售质量安全监督管理办法》等规定，于
									{{ checkData.CHECK_TIME |
										dateFilter }}对你单位进行了监督检查。本次监督检查按照 《 {{ checkData.TEMPLATE_NAME }} 》
									开展，检查了（　{{ checkData.contentNum }}　　）项内容：其中：
									重点项（　　　{{ checkData.importNum }}　　）项，项目序号分别是（　　　　{{ checkData.importNo }}　　　），
									发现问题（　　　{{ checkData.importProblemNum }}　　）项，项目序号分别是（　　　　　{{
										checkData.importProblemNo }}　　　）；
									一般项（　　　{{ checkData.commonlyNum }}　　）项，项目序号分别是（　　{{ checkData.commonlyNo }}　　），
									发现问题（　　　{{ checkData.commonlyProblemNum }}　　）项，项目序号分别是（　　　　{{
										checkData.commonlyProblemNo
									}}　　　　）。
								</text>
							</td>
						</tr>
						<tr>
							<td class="content-td" colspan="4">
								<text class="check-content">
									<div>检查结果：{{ checkData.CHECK_RESULTS }}</div><br>

									<div>处理结果：{{ checkData.COPE_RESULT | processResultFilter }}<span
											v-if="resultTime !== undefined && resultTime !== '' && resultTime !== null">（{{
												resultTime
											}}前整改到位）</span></div><br>


									<div>说明： {{ checkData.OTHER_PROBLEM == 'undefined' ? ' ' : checkData.OTHER_PROBLEM
										}}</div>
								</text>
							</td>
						</tr>
						<tr>
							<td class="content-td" colspan="2">
								<view>检查人员（签名）：<image class="signature-image"
										:src="sigPic == null ? '/static/img/sing.png' : sigPic" @tap="sign(1)"> </image>
								</view>
								<view>检查人员（签名）：<image class="signature-image"
										:src="sigPic2 == null ? '/static/img/sing.png' : sigPic2" @tap="sign(2)">
									</image>
								</view>
								<view style="text-align: right;"><text> {{ formattedDate }}</text> </view>
							</td>
							<td class="content-td" colspan="2"
								style="flex-direction: column;justify-content: space-between;">
								<view>法定代表人或负责人（签名）： </view>
								<image class="signature-image" :src="sigPic3 == null ? '/static/img/sing.png' : sigPic3"
									@tap="sign(3)"> </image>
								<view style="text-align: right;margin-top: 10px;"><text> {{ formattedDate }}</text>
								</view>
							</td>
						</tr>
						<tr>
							<td class="content-td">备注</td>
							<td class="content-td" colspan="3">
								被检查单位为食品经营者的填写许可证编号，固定食品制售摊贩、早餐车填写备案证明编号
								，其他业态类型的填写社会信用代码；日常监督检查结果为不符合，如发现涉及食品安全违法行为的，应依法进行立案调查处理；
								如涉及移送情形的，应依法移送相关有权处理部门。结果处理应使用规范的法律文书；文中各类检查要点表与其风险分级动态评级表为同一张表。
							</td>
						</tr>
					</table>
				</view>
			</scroll-view>
		</view>
		<screenshot ref="screenshot" :screenId="'scrrent-page'" @renderFinish="getScreenImg"></screenshot>
	</view>
</template>

<script>
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const day = String(currentDate.getDate()).padStart(2, '0');
const formattedDate = `${year}年${month}月${day}日`;
import longscreen from '@/components/screenShot.vue'
import commonNavBar from '@/components/CommonNavBar.vue';

export default {
	components: {
		commonNavBar,
		'screenshot': longscreen
	},
	data() {
		return {
			checkData: {
				standardList: []
			},
			sigPic: null,
			sigPic2: null,
			sigPic3: null,
			checkId: null,
			contentHeight: 0,
			formattedDate: formattedDate,
			resultTime: null
		}
	},
	filters: {
		selectionFilter(value) {

			if (value == '1') {
				return '是'
			} else if (value == '2') {
				return '否'
			} else if (value == '3') {
				return '合理缺项'
			} else {
				return '';
			}

		},
		dateFilter(value) {
			if (value == undefined || value == 'undefined') {
				return ''
			} else {
				if (value.length <= 4) {
					return value;
				}
				const valueArr = value.split("-");
				return valueArr[0] + '年' + valueArr[1] + '月' + valueArr[2] + '日'
			}
		},
		processResultFilter(value) {
			if (value == undefined || value == 'undefined') {
				return ''
			} else {
				//1通过检查，2责令整改，3调查处理
				if (value == '1') {
					return '通过检查';
				} else if (value == '2') {
					return '责令整改';
				} else if (value == '3') {
					return '调查处理';
				}
			}
		}
	},
	onLoad(options) {
		this.checkId = options.id
		this.resultTime = options.time

	},
	onReady() {
		const that = this;
		this.initLoadData();
		uni.createSelectorQuery().in(this).select("#content").fields({
			rect: true
		}, function (res) {
			that.contentHeight = uni.getSystemInfoSync().windowHeight - res.top
		}).exec();
	},
	methods: {
		goBack() {
			uni.navigateBack();
		},
		async initLoadData() {
			//加载页面所需要数据	
			uni.showLoading({
				title: '获取数据中',
				mask: true
			})
			const res = await uni.$api.getRiskResultTable({
				checkId: this.checkId
			})

			if (res.data.code == '1') {
				uni.hideLoading();
				uni.showToast({
					title: '获取信息失败',
					icon: 'error'
				})
				return;
			}
			this.checkData = res.data.data

			// 关键处理：如果处理结果为“通过检查”，则resultTime设为空
			const processResult = this.$options.filters.processResultFilter(this.checkData.COPE_RESULT);
			if (processResult === '通过检查') {
				this.resultTime = '';
			}

			uni.hideLoading();
		},
		saveImage() {
			if (this.sigPic == null || this.sigPic2 == null || this.sigPic3 == null) {
				uni.showToast({
					icon: 'error',
					title: '请先进行签名'
				})
				return;
			}
			this.$refs.screenshot.startScreen();
		},
		getScreenImg(ret) {
			const that = this;
			//根据传输过来的id 和选中的图片传到后台直接保存 
			uni.showLoading({
				title: '正在生成',
				mask: true
			})
			const uploadTask = uni.uploadFile({
				url: '/riskManager/uploadResultRecord', //仅为示例，非真实的接口地址
				filePath: ret.posterPath,
				name: "imgFile",
				timeout: '10000', //超时时间10秒
				formData: {
					"checkId": that.checkId,
					"type": '2'
				},
				dataType: 'json',
				success: (upRes) => {
					uni.showToast({
						title: '生成完成',
						icon: 'success'
					});
					console.log(upRes.data)
					const result = JSON.parse(upRes.data);
					if (result.code == '1') {
						uni.showModal({
							content: '保存失败,请重新尝试',
							showCancel: false
						})
					}
					console.log(result.data)
					const eventChannel = that.getOpenerEventChannel();
					eventChannel.emit('yaodianbiao', result.data);
					uni.navigateBack();
				},
				fail(err) {
					uni.showModal({
						content: '保存失败,请重新尝试',
						showCancel: false
					})
				},
				complete() {
					uni.hideLoading()
				}
			});
		},
		sign(type) {
			const that = this;
			//签名
			uni.navigateTo({
				url: '/pages/signature/signature',
				events: {
					updateSign(data) {
						if (type == 1) {
							that.sigPic = data;
						} else if (type == 2) {
							that.sigPic2 = data;
						} else if (type == 3) {
							that.sigPic3 = data;
						}
					}
				}
			})
		}
	}
}
</script>

<style lang="scss">
.addInspectTabsContent {
	background: #F9F9F9;
	height: calc(100vh - 195rpx);
	height: 100vh;
	padding: 14px;

	.upper {
		position: fixed;
		z-index: 1;
		top: 0;
		left: 0;
		width: 100%;

		.title {
			background: url('../../static/img/inspectBg.png');
			background-size: cover;
			width: 100%;
			height: 115rpx;
			line-height: 115rpx;
			position: relative;
			display: flex;
			justify-content: center;
			color: #ffffff;

			.arrow {
				position: absolute;
				left: 33rpx;
				top: 50%;
				transform: translate(0%, -50%);
				width: 100rpx;
				height: 100rpx;

				&::before {
					display: block;
					content: "";
					position: absolute;
					top: 50%;
					left: 0%;
					width: 20rpx;
					height: 20rpx;
					border-left: 1px solid #fff;
					border-top: 1px solid #fff;
					transform: translate(0%, -50%) rotate(-45deg);
				}
			}

			text {
				font-size: 30rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				letter-spacing: 1px;
			}

		}
	}
}

.intelligence {
	position: absolute;
	right: 30rpx;
	top: 35rpx;
	display: flex;
	align-items: center;
	justify-content: center;

}

.list {
	padding-top: 20px;
	padding-bottom: 180px;
	overflow-y: auto;
}

.title-view {
	width: 750rpx;
	text-align: center;
}

.title-text {
	font-size: 20rpx;
	color: black;
	font-weight: 700;

}

.content-table {
	width: 750rpx;


}

.content-td {
	border: 1px solid #8a8a8a;
	padding: 10px;
}

.signature-image {
	width: 100px;
	height: 100px;
}

.check-content {
	word-break: break-all;
	word-spacing: 4px;
}
</style>